/**
 * 日期格式化
 * @param {String} date 年-月-日 时:分:秒
 * @returns String
 */
 function dateFormat(date){
    var oldDate = new Date(date)  //将传进来的字符串转为标准时间
    var newDate = new Date()      //获取本机当前时间
    var minute = 1000 * 60
    var hour = minute * 60
    var day = hour * 24
    var month = day * 30
    var year = day * 365
    var dateStr
    var diff = newDate.getTime() - oldDate.getTime()
    if (diff <= minute) {   // 小于一分钟展示为刚刚
        dateStr = '刚刚'
    } else if (minute < diff && diff <= hour) {         // 大于一分钟小于一小时展示为分钟
        dateStr = Math.round((diff / minute)) + '分钟前'
    } else if (hour < diff && diff <= day) {            // 大于一小时小于一天展示为小时
        dateStr = Math.round((diff / hour)) + '小时前'
    } else if (day < diff && diff <= month) {           // 大于一天小于三十天展示为天
        dateStr = Math.round((diff / day)) + '天前'
    } else if(month < diff && diff <= year){            // 大于三十天展示为天小于一年展示为月
        dateStr = Math.round((diff / month)) + '月前'
    } else {                                            // 大于一年展示为年
        dateStr = Math.round((diff / year)) + '年前'
    }                                               
    return dateStr
}
/**
 * 字符串补0(不足两位数时)
 * @param {String|Number} data 比如：日期数据 
 * @returns 过滤后的数据
 */
function zeroize(data){
    return String(data).length === 1 ? '0'+data : data
}
/**
 * 快速获取当前时间
 * @param {String} str 比如：YmdHis Y/m/d
 * @returns String
 */
function format(str){
    var d = new Date()
    str = str.replaceAll('Y',d.getFullYear())
    str = str.replaceAll('m',zeroize(d.getMonth()+1))
    str = str.replaceAll('d',zeroize(d.getDate()))
    str = str.replaceAll('H',zeroize(d.getHours()))
    str = str.replaceAll('i',zeroize(d.getMinutes()))
    str = str.replaceAll('s',zeroize(d.getSeconds()))
    return str
}
/**
 * 随机颜色
 * @returns String
 */
function randomColor(){
    var colorArr = [0,1,2,3,4,5,6,7,8,9,'A','B','C','D','E','F']
    var min = 0
    var max = colorArr.length-1
    var first = colorArr[Math.floor(Math.random()*(max-min+1)+min)]
    var two = colorArr[Math.floor(Math.random()*(max-min+1)+min)]
    var three = colorArr[Math.floor(Math.random()*(max-min+1)+min)]
    var four = colorArr[Math.floor(Math.random()*(max-min+1)+min)]
    var five = colorArr[Math.floor(Math.random()*(max-min+1)+min)]
    var six = colorArr[Math.floor(Math.random()*(max-min+1)+min)]
    return '#'+first+two+three+four+five+six
}
/**
 * 判断浏览器内核
 * @returns String
 */
function browser(){
    var u = navigator.userAgent
        if(u.indexOf('Trident') > -1) return 'IE内核'
        if(u.indexOf('Presto') > -1) return 'opera内核'
        if(u.indexOf('AppleWebKit') > -1) return '苹果、谷歌内核'
        if(u.indexOf('Firefox') > -1) return '火狐内核Gecko'
}
/**
 * 判断是移动端还是PC端
 * @returns String
 */
function browserPc(){
    var ed = navigator.userAgent
    if(ed.indexOf('iPhone') > -1 || ed.indexOf('Android') > -1 || ed.indexOf('iPad') > -1) return '移动端'
    else return '电脑端'
}
/**
 * 判断是否是微信浏览器
 * @returns String
 */
function browserWx(){
    if(navigator.userAgent.indexOf('MicroMessenger') > -1) return '微信浏览器' 
    else return '不是微信浏览器'
}
/**
 * 获取标签非行内样式
 * @param {Object} tagObj // 标签对象
 * @param {String} cssAttr // CSS属性值
 * @returns String
 */
function getStyle(tagObj,cssAttr){
    if(window.getComputedStyle){
        return getComputedStyle(tagObj)[cssAttr]
    } else {
        return tagObj.currentStyle[cssAttr]
    }
}
/**
 * 获取url参数
 * @param {String} key 
 * @returns String
 */
function getParams(key) {
    let regexp = new RegExp(`(^|&)${key}=([^&]*)(&|$)`)
    let str = decodeURI(window.location.href.split('?').pop()).match(regexp)
    return str != null ? str[2] : null
}